package cn.stylefeng.guns.modular.system.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.entity.Store;
import cn.stylefeng.guns.modular.system.mapper.StoreMapper;
import cn.stylefeng.guns.modular.system.model.params.StoreParam;
import cn.stylefeng.guns.modular.system.model.result.StoreResult;
import  cn.stylefeng.guns.modular.system.service.StoreService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 门店表 服务实现类
 * </p>
 *
 * @author 小飞
 * @since 2019-07-11
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements StoreService {

    //申请门店表
    @Autowired
    private StoreMapper storeMapper;


    @Override
    public void add(StoreParam param){
        Store entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(StoreParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(StoreParam param){
        Store oldEntity = getOldEntity(param);
        Store newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public StoreResult findBySpec(StoreParam param){
        return null;
    }

    @Override
    public List<StoreResult> findListBySpec(StoreParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(StoreParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(StoreParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Store getOldEntity(StoreParam param) {
        return this.getById(getKey(param));
    }

    private Store getEntity(StoreParam param) {
        Store entity = new Store();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }


    /**
     * 查询所有的门店和申请人
     */
    @Override
    public List<Store> QueryStoreAll(String condition){
        return storeMapper.QueryStoreAll(condition);
    }


    /**
     * 查询最大值
     */
    @Override
    public Long QueryStoreMax(){
        return storeMapper.QueryStoreMax();
    }


    /**
     * 根据 store_id 查询 2 表的详情
     */
    @Override
    public List<StoreResult> QueryTwoId(@Param("storeId") Long storeId){
        return storeMapper.QueryTwoId(storeId);
    }


    /**
     *根据 store_id 修改数据
     */
    @Override
    public int UpdateStoreId(StoreParam sPm){
        return storeMapper.UpdateStoreId(sPm);
    }


    /**
     * 根据申请人 id 的主键查询 查询 2 表的详情QueryUpShopId
     */
    @Override
    public List<StoreResult> QueryUpShopId(@Param("id") Long id){
        return storeMapper.QueryUpShopId(id);
    }













}
